
package zhr.chapter5.MemberCardDemoApp.pospackage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class POSView {
    private JPanel panel1;
    private JTextField 应付金额TextField;
    private JTextField 消费金额TextField;
    private JTextField textField10;
    private JRadioButton memberCard9折RadioButton;
    private JRadioButton VIPCard7折RadioButton;
    private JRadioButton superVIPCard5折RadioButton;
    private JButton 刷卡消费Button;
    private JTextField 消费结果TextField;
    private JTextField 当前余额TextField1;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField4;
    private JTextField textField3;

    public POSView() {
        // 初始化 JFrame
        JFrame frame = new JFrame("POS System");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());

        // 初始化面板
        panel1 = new JPanel();
        panel1.setLayout(new GridLayout(0, 2, 5, 5)); // 使用 GridLayout 布局

        // 初始化文本字段和标签
        应付金额TextField = new JTextField(10);
        消费金额TextField = new JTextField(10);
        textField10 = new JTextField(10);
        textField1 = new JTextField(10);
        textField2 = new JTextField(10);
        textField3 = new JTextField(10);
        textField4 = new JTextField(10);
        当前余额TextField1 = new JTextField(10);
        消费结果TextField = new JTextField(10);

        // 初始化单选按钮并添加到 ButtonGroup
        ButtonGroup cardGroup = new ButtonGroup();
        memberCard9折RadioButton = new JRadioButton("Member Card 9折", true);
        VIPCard7折RadioButton = new JRadioButton("VIP Card 7折", false);
        superVIPCard5折RadioButton = new JRadioButton("Super VIP Card 5折", false);
        cardGroup.add(memberCard9折RadioButton);
        cardGroup.add(VIPCard7折RadioButton);
        cardGroup.add(superVIPCard5折RadioButton);

        // 初始化按钮并添加事件监听器
        刷卡消费Button = new JButton("刷卡消费");
        刷卡消费Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                calculateDiscount();
            }
        });

        // 将组件添加到面板
        panel1.add(new JLabel("消费金额:"));
        panel1.add(消费金额TextField);
        panel1.add(new JLabel("应付金额:"));
        panel1.add(应付金额TextField);
        panel1.add(memberCard9折RadioButton);
        panel1.add(VIPCard7折RadioButton);
        panel1.add(superVIPCard5折RadioButton);
        panel1.add(superVIPCard5折RadioButton);
        panel1.add(刷卡消费Button);
        panel1.add(new JLabel("消费结果:"));
        panel1.add(消费结果TextField);
        panel1.add(new JLabel("当前余额:"));
        panel1.add(当前余额TextField1);

        // 将面板添加到 JFrame
        frame.add(panel1, BorderLayout.CENTER);

        // 设置窗口大小和可见性
        frame.setSize(400, 300);
        frame.setVisible(true);
    }

    private void calculateDiscount() {
        try {
            double amount = Double.parseDouble(消费金额TextField.getText());
            double discount = 1.0;
            if (memberCard9折RadioButton.isSelected()) {
                discount = 0.9;
            } else if (VIPCard7折RadioButton.isSelected()) {
                discount = 0.7;
            } else if (superVIPCard5折RadioButton.isSelected()) {
                discount = 0.5;
            }
            double 应付金额 = amount * discount;
            应付金额TextField.setText(String.valueOf(应付金额));
            // 假设当前余额是固定的，这里只是示例
            当前余额TextField1.setText("1000.00");
            消费结果TextField.setText("消费成功");
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "请输入有效的消费金额", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    public static void main(String[] args) {
        // 在事件分发线程中创建和显示这个界面
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new POSView();
            }
        });
    }
}